#!/usr/bin/env node

const fs = require('fs')
const inquirer = require('inquirer')
const path = require('path')
const ejs = require('ejs')

const queList = [{
        type: 'input',
        message: '请输入项目名',
        name: 'projectName',
        validate(an) {
            return an ? true : '此项为必填项'
        }
    },
    {
        type: 'input',
        message: '标题',
        name: 'title',
    },
    {
        type: 'list',
        message: '请选择需要',
        choices: ['about', 'home'],
        name: 'method',
    },
]

inquirer.prompt(queList).then(answer => {
    const temp = path.join(process.cwd(), "temp")
    const dist = process.cwd()
    fs.readdir(temp, (err, files) => {
        if(err) throw err
        files.forEach(item => {
            ejs.renderFile(path.join(temp, item), answer, (err, result) => {
                if(err) throw err
                fs.writeFileSync(path.join(dist, '/test/' + item), result)
            })
        })
    })
}).catch(e => {
    throw e
})